#!/usr/bin/env bash
# ----------------------------------------------------------------------
# Filename	:  01-wpd.sh
# Version	:  2.0
# Date		:  2024/10/10
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinsec.com.cn
# History	:
#
# Function	:  弱密码检测
# Out		:
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="弱密码检测"

testuser1_wpd="ltfwpd01"
passwd1_wpd="olleH717.12.#$"
userip_audit01="localhost"
AddUserNames_LTFLIB="${testuser1_wpd}"
AddUserPasswds_LTFLIB="${passwd1_wpd}"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB() {
    wpd_john="john-1.9.0.tar.gz"
    wpd_john_file="${SEC_SRC_DIR_SEC}/${wpd_john}"
    cp "${wpd_john_file}" "${TmpTestDir_LTFLIB}"
    CommRetParse_FailDiy_LTFLIB ${ERROR} "cp ${wpd_john_file} ${TmpTestDir_LTFLIB}"
    return $TPASS
}

## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB() {
    return $TPASS
}

## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB() {
    testcase_1
    return $TPASS
}

## TODO ： 弱密码检测
#
testcase_1() {
    tar -zxvf "${TmpTestDir_LTFLIB}/${wpd_john}" -C "${TmpTestDir_LTFLIB}" >/dev/null
    CommRetParse_LTFLIB "tar -zxvf ${TmpTestDir_LTFLIB}/${wpd_john} -C ${TmpTestDir_LTFLIB} >/dev/null"

    cd "${TmpTestDir_LTFLIB}/john-1.9.0/src"
    CommRetParse_LTFLIB "cd ${TmpTestDir_LTFLIB}/john-1.9.0/src"
    local archtype=$(arch)
    if [ "${archtype}" == "x86_64" ]; then
        make linux-x86-64 >/dev/null
        CommRetParse_LTFLIB "make linux-x86-64 >/dev/null"
    else
        make linux-arm64le >/dev/null
        CommRetParse_LTFLIB "make linux-arm64le >/dev/null"
    fi

    cp "/etc/shadow" "${TmpTestDir_LTFLIB}/shadow.txt"
    CommRetParse_LTFLIB "cp /etc/shadow ${TmpTestDir_LTFLIB}/shadow.txt"

    cd "${TmpTestDir_LTFLIB}/john-1.9.0/run"
    CommRetParse_LTFLIB "cd ${TmpTestDir_LTFLIB}/john-1.9.0/run"

    ./john "${TmpTestDir_LTFLIB}/shadow.txt" --wordlist=password.lst &>"${TmpTestDir_LTFLIB}/wpd_check.log"
    CommRetParse_LTFLIB "./john ${TmpTestDir_LTFLIB}/shadow.txt --wordlist=password.lst &>${TmpTestDir_LTFLIB}/wpd_check.log"

    cat "${TmpTestDir_LTFLIB}/wpd_check.log" | grep -v "Loaded" | grep "("
    CommRetParse_LTFLIB "cat ${TmpTestDir_LTFLIB}/wpd_check.log | grep -v Loaded |grep '('" "False" "yes"
}

#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
